package cn.onesdream.app.api.service.Impl;

import cn.onesdream.app.api.service.PlanetService;
import cn.onesdream.app.dao.entity.Planet;
import cn.onesdream.app.dao.entity.User;
import cn.onesdream.app.dao.mapper.PlanetMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/2/9 23:00
 */
@Service
public class PlanetServiceImpl extends ServiceImpl<PlanetMapper, Planet> implements PlanetService {

    @Override
    public List<Planet> getPlanetByUserId(Integer userId) {
        List<Planet> planetList = getPlanetByOwnUserId(userId);
        planetList.addAll(getPlanetByShareUserId(userId));
        return planetList;
    }

    @Override
    public List<Planet> getPublicPlanet(Integer userId) {
        //获取用户创建的公开星球列表
        QueryWrapper<Planet> wrapper = new QueryWrapper<>();
        wrapper.eq("own_user_id", userId).eq("private_id", 0);
        List<Planet> ownPlanetList = baseMapper.selectList(wrapper);
        //获取用户参与的公开星球列表
        String sql = "select share_user_id from planet where JSON_CONTAINS(share_user_id->'$[*]', '\"" + userId + "\"', '$') and private_id = 0";
        wrapper.clear();
        wrapper.inSql("share_user_id", sql);
        List<Planet> sharePlanetList = baseMapper.selectList(wrapper);
        //合并两个星球列表
        List<Planet> planetList = new ArrayList<>();
        planetList.addAll(ownPlanetList);
        planetList.addAll(sharePlanetList);
        return planetList;
    }

    @Override
    public List<Planet> getRandomPlanet(Byte count) {
        //随机查找公开状态的count条有效星球数据
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        String sql = "select p.id from (select id from planet where private_id = 0 and active = 1 and own_user_id <> "+ userId + " order by rand() limit "+ count +")as p";
        QueryWrapper<Planet> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", sql);
        List<Planet> planetList = baseMapper.selectList(wrapper);
        return planetList;
    }

    @Override
    public void deletePlanet(Integer planetId) {
        baseMapper.deleteById(planetId);
    }

    @Override
    public Planet getPlanet(Integer planetId) {
        return baseMapper.selectById(planetId);
    }

    @Override
    public Planet changePlanetInfo(Integer planetId, String name, String description, String shareUserId, Byte privateId) {
        UpdateWrapper<Planet> planetUpdateWrapper = new UpdateWrapper<>();
        Planet planet = new Planet();
        planet.setName(name);
        planet.setDescription(description);
        planet.setShareUserId(shareUserId);
        planet.setPrivateId(privateId);
        planetUpdateWrapper.lambda()
                .eq(Planet::getId, planetId);
        baseMapper.update(planet, planetUpdateWrapper);
        planet = baseMapper.selectById(planetId);
        return planet;
    }

    @Override
    public List<Planet> getPlanetByShareUserId(Integer shareUserId){

        String sql = "select share_user_id from planet where  JSON_CONTAINS(share_user_id->'$[*]', '\"" + shareUserId + "\"', '$')";
        QueryWrapper<Planet> wrapper = new QueryWrapper<>();
        wrapper.inSql("share_user_id", sql);
        List<Planet> planetList = baseMapper.selectList(wrapper);
        return planetList;
    }

    @Override
    public List<Planet> getPlanetByOwnUserId(Integer ownUserId) {
        QueryWrapper<Planet> wrapper = new QueryWrapper<>();
        wrapper.eq("own_user_id", ownUserId);
        List<Planet> planetList = baseMapper.selectList(wrapper);
        return planetList;
    }

    @Override
    public void addPlanet(String name, String description, Integer ownUserId, String shareUserId, Byte typeId, Byte privateId) {
        Planet planet = new Planet();
        planet.setName(name);
        planet.setDescription(description);
        planet.setOwnUserId(ownUserId);
        planet.setShareUserId(shareUserId);
        planet.setTypeId(typeId);
        planet.setPrivateId(privateId);
        baseMapper.insert(planet);
    }
}
